package ldh.serialize.type;

import java.io.IOException;

import ldh.serialize.BinarySerialized;
import ldh.serialize.io.ByteBuf;
import ldh.serialize.util.SerializedUtil;

public class CharSerialized implements BinarySerialized<Character> {

	@Override
	public void write(ByteBuf out, Character num) throws IOException {
		if (num == null) {
			SerializedUtil.writeByte(out, (byte)0);
			return;
		}
		SerializedUtil.writeByte(out, (byte)1);
		byte b = (byte)(0xff & (num));
		SerializedUtil.writeByte(out, b);
	}

	@Override
	public Character read(ByteBuf in) throws IOException {
		byte isNull = SerializedUtil.readByte(in);
		if (isNull < 1) {
			return null;
		}
		return in.readChar();
	}

	@Override
	public Class<Character> getClassType() {
		return Character.class;
	}

}
